﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#pragma execution_character_set("utf-8")
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->groupBox->setStyleSheet("QGroupBox { font-weight: bold;color: #FFFFFF } ");
    m_thread = new AlgoThread();
    m_thread->start();






    myWorld=GameWorld::getHandle()->myWorld;
//    osg_widget=ui->openGLWidget;
    osg_widget=ui->openGLWidget;
    myVehicle=GameWorld::getHandle()->myVehicle;
    viewer=osg_widget->getViewer();
    GameWorld::getHandle()->setViewer(viewer);


//    这里貌似没有什么用
//    btosgVec3 up(0., 0., 1.);
    osg::Matrix matrix;
//    matrix.makeLookAt(osg::Vec3(0.,8.,5.), osg::Vec3(0.,0.,1.), up);

    viewer->getCamera()->setViewMatrix(matrix);


    osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
    osg::ref_ptr<osg::Image> image = osgDB::readImageFile( "assets/img/xingKong02.jpeg" );
    texture->setImage( image.get() );

    osg::ref_ptr<osg::Drawable> quad = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(1.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 1.0f, 0.0f) );
    quad->getOrCreateStateSet()->setTextureAttributeAndModes( 0,texture.get());

    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable( quad.get() );

    //这里是我的背景
    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
    camera->setClearMask( 0 );
    camera->setCullingActive( false );
    camera->setAllowEventFocus( false );
    camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
    camera->setRenderOrder( osg::Camera::POST_RENDER );
    camera->setProjectionMatrix( osg::Matrix::ortho2D(0.0, 1.0, 0.0, 1.0) );
    camera->addChild( geode.get() );

    osg::StateSet* ss = camera->getOrCreateStateSet();
    ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
    ss->setAttributeAndModes( new osg::Depth(osg::Depth::LEQUAL, 1.0, 1.0) );
    myWorld->scene->addChild(camera.get());






    viewer->getCamera()->addChild( myWorld->scene );

    viewer->setCameraManipulator( GameWorld::getHandle()->manipulator1 );
    viewer->addEventHandler(new FollowUpdater(myVehicle->pNode));
//    viewer->setSceneData(myWorld->scene);


    //创建主场景的副视角
    CreateSubwid();



    timer=new QTimer(this);
    realTimer=new QElapsedTimer;
    connect(timer, &QTimer::timeout, this, QOverload<>::of(&MainWindow::update));
    timer2 = new QTimer(this);


    connect(timer2, &QTimer::timeout, this, QOverload<>::of(&MainWindow::realTimeUpdate));

    timer->start(16);
    timer2->start(5);

    timer3 = new QTimer(this);
    connect(timer3, &QTimer::timeout, this, QOverload<>::of(&MainWindow::CalTraj));
    timer2->start(0);


    realTimer->start();

    //下面在进行一些绑定
    //最开始是模式的转换
    //只要按下按钮，就开始读取键盘数据
    connect(ui->action_manCtl, SIGNAL(triggered()), this, SLOT(SlotStartThread()));
    //这个连接的就是按钮和通过算法自动规划路径的开关
    connect(ui->action_selfCtl, SIGNAL(triggered()), this, SLOT(changePurepursuit()));



}

MainWindow::~MainWindow()
{
    delete ui;

}

void MainWindow::update()
{

    GameWorld::getHandle()->update();

}

void MainWindow::realTimeUpdate()
{
    dt = realTimer->elapsed() - lastTime;

    myWorld->stepSimulation(dt,1);

    lastTime = realTimer->elapsed();
}

void MainWindow::CreateSubwid()
{
    osg_widget1 = new QtOSGWidget();

    osg_widget1 = ui->openGLWidget_2;

    viewer1 = osg_widget1->getViewer();

    GameWorld::getHandle()->setViewer(viewer1);

//    btosgVec3 up(0., 0., 1.);
//    osg::Matrix matrix;
//    viewer->getCamera()->setViewMatrix(matrix);

    viewer1->getCamera()->addChild( myWorld->scene );

    viewer1->setCameraManipulator( GameWorld::getHandle()->manipulator );

    viewer1->addEventHandler(new FollowUpdater(myVehicle->pNode));
}

void MainWindow::SlotStartThread()
{
    timer3->stop();
    myVehicle->setMass(800.);
    if(threadOsg.joinable())
    {
        threadOsg.detach();
    }
}

void MainWindow::changePurepursuit()
{
    myVehicle->test(btosgVec3(0,0,0.5));
    myVehicle->vehicle->applyEngineForce(5000, 2);
    myVehicle->vehicle->applyEngineForce(5000, 3);
    myVehicle->vehicle->setSteeringValue(btScalar(0), 0);
    myVehicle->vehicle->setSteeringValue(btScalar(0), 1);
    ab = new purepursuit(myVehicle);
}

void MainWindow::CalTraj()
{
    if(ab->end == 0)
    {

        myVehicle->vehicle->setBrake(1000000000,2);
        myVehicle->vehicle->setBrake(1000000000,3);
        ab->PidControl(0);

    }else
    {


         ab->start_pursuit(SignalFactory::SignalInstance()->locateX,SignalFactory::SignalInstance()->locateY,myVehicle);
    }

}

